﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="RawMaterials.aspx.cs" Inherits="XYZ_Inventory.RawMaterials" Culture="si-LK" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link href="Styles/GridView.css" rel="stylesheet" type="text/css"/>
    <link href="Styles/SearchBox.css" rel="stylesheet" type="text/css"/>
    <link href="Styles/DetailsView.css" rel="stylesheet" type="text/css"/>
    <link href="Styles/ValidationSummary.css" rel="stylesheet" type="text/css"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div style="padding: 0 1em 1em 1em;">
<div class="searchBoxContainer">
    <asp:TextBox ID="txtSearch" CssClass="searchBox" runat="server" placeholder="Search by Name"></asp:TextBox>
    <asp:ImageButton ID="Button1" runat="server" AlternateText="Search" ImageUrl="~/Images/magnifying_glass_icon.png"/>
</div>
<div class="gridViewContainer">
    <asp:GridView ID="GridView1" runat="server" CssClass="gridView" CellPadding="4" ForeColor="#333333"
                  GridLines="None" AutoGenerateColumns="False" DataKeyNames="RawMaterialID" AllowPaging="True"
                  AllowSorting="True" DataSourceID="RawMaterialSource">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775"/>
        <Columns>
            <asp:BoundField DataField="RawMaterialID" HeaderText="Material ID" ReadOnly="True"
                            SortExpression="RawMaterialID">
                <ItemStyle HorizontalAlign="Right"/>
            </asp:BoundField>
            <asp:TemplateField HeaderText="Material Name" SortExpression="RawMaterialName">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" MaxLength="50" Text='<%# Bind("RawMaterialName") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Raw material's name is required."
                                                Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Edit">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("RawMaterialName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Material Type" SortExpression="RawMaterialType">
                <ItemTemplate>
                    <%# Eval("RawMaterialType") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList runat="server" ID="ddlEditMaterial" SelectedValue='<%# Bind("RawMaterialType") %>'
                                      DataSourceID="RawMaterialTypeSource" DataTextField="Type" DataValueField="Type">
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="ddlEditMaterial" ErrorMessage="Raw material's type is required."
                                                Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Edit">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description" SortExpression="Description">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" MaxLength="250" TextMode="MultiLine" Text='<%# Bind("Description") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox2" ErrorMessage="Description is required."
                                                Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Edit">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("Description").ToString().Replace(Environment.NewLine, "<br/>") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Unit Buying Price" SortExpression="UnitBuyingPrice">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" TextMode="Number" min="1" Text='<%# Bind("UnitBuyingPrice") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox3" ErrorMessage="Unit Buying Price is required."
                                                Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Edit">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("UnitBuyingPrice", "{0:c}") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Right"/>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Reorder Level" SortExpression="ReorderLevel">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" TextMode="Number" min="1" Text='<%# Bind("ReorderLevel") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox4" ErrorMessage="Reorder Level is required."
                                                Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Edit">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("ReorderLevel") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Right"/>
            </asp:TemplateField>
            <asp:BoundField DataField="Stock" HeaderText="Stock" SortExpression="Stock" ReadOnly="True">
                <ItemStyle HorizontalAlign="Right"/>
            </asp:BoundField>
            <asp:TemplateField HeaderText="Actions" ShowHeader="False">
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                    ValidationGroup="Edit" Text="Update">
                    </asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                          Text="Cancel">
                    </asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                    Text="Edit">
                    </asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                                          Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');">
                    </asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataRowStyle CssClass="emptyDataRow"></EmptyDataRowStyle>
        <EmptyDataTemplate>
            <table class="gridView" id="ContentPlaceHolder1_GridView1" style="border-collapse: collapse; color: #333333; width: 63em;">
                <tr style="background-color: #5D7B9D; color: White; font-weight: bold;">
                    <th scope="col">
                        Material ID
                    </th>
                    <th scope="col">
                        Material Name
                    </th>
                    <th scope="col">
                        Material Type
                    </th>
                    <th scope="col">
                        Description
                    </th>
                    <th scope="col">
                        Unit Buying Price
                    </th>
                    <th scope="col">
                        Reorder Level
                    </th>
                    <th scope="col">
                        Stock
                    </th>
                    <th scope="col">
                        Actions
                    </th>
                </tr>
                <tr style="background-color: #F7F6F3; color: #333333;">
                    <td colspan="8">
                        NO RAW MATERIAL FOUND
                    </td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <EditRowStyle BackColor="#999999"/>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"/>
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333"/>
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"/>
        <SortedAscendingCellStyle BackColor="#E9E7E2"/>
        <SortedAscendingHeaderStyle BackColor="#506C8C"/>
        <SortedDescendingCellStyle BackColor="#FFFDF8"/>
        <SortedDescendingHeaderStyle BackColor="#6F8DAE"/>
    </asp:GridView>
    <asp:ValidationSummary ID="ValidationSummary2" Style="margin-top: 1em;" CssClass="validationSummary"
                           HeaderText="<b>Please fix following error(s):</b>" ValidationGroup="Edit" ForeColor="Red"
                           runat="server"/>
</div>
<asp:EntityDataSource ID="RawMaterialSource" runat="server" ConnectionString="name=XYZDBContext"
                      DefaultContainerName="XYZDBContext" EnableDelete="True" EnableFlattening="False"
                      EnableInsert="True" EnableUpdate="True" EntitySetName="RawMaterialInventories"
                      EntityTypeFilter="RawMaterialInventory" OrderBy="it.RawMaterialID DESC" Where="it.RawMaterialName LIKE '%'+@RawMaterialName +'%' OR @RawMaterialName is NULL">
    <WhereParameters>
        <asp:ControlParameter ControlID="txtSearch" DbType="String" Name="RawMaterialName"
                              PropertyName="Text"/>
    </WhereParameters>
</asp:EntityDataSource>
<br/>
<asp:DetailsView ID="DetailsView1" CssClass="detailsView" runat="server" CellPadding="4"
                 ForeColor="#333333" GridLines="None" Height="50px" Width="125px" AutoGenerateRows="False"
                 DataKeyNames="RawMaterialID" DataSourceID="RawMaterialSource" DefaultMode="Insert"
                 OnItemInserted="DetailsView1_ItemInserted" HeaderText="Register a raw material">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775"/>
    <CommandRowStyle BackColor="#E2DED6" Font-Bold="True"/>
    <EditRowStyle BackColor="#999999"/>
    <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True"/>
    <Fields>
        <asp:BoundField DataField="RawMaterialID" HeaderText="Material ID" ReadOnly="True"
                        SortExpression="RawMaterialID" InsertVisible="False"/>
        <asp:TemplateField HeaderText="Material Name" SortExpression="RawMaterialName">
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" MaxLength="50" Text='<%# Bind("RawMaterialName") %>'></asp:TextBox>
                <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Raw material's name is required."
                                            Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert">
                </asp:RequiredFieldValidator>
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Material Type">
            <InsertItemTemplate>
                <asp:DropDownList runat="server" ID="ddlMaterial" SelectedValue='<%# Bind("RawMaterialType") %>'
                                  DataSourceID="RawMaterialTypeSource" DataTextField="Type" DataValueField="Type">
                </asp:DropDownList>
                <asp:RequiredFieldValidator runat="server" ControlToValidate="ddlMaterial" ErrorMessage="Raw material's type is required."
                                            Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert">
                </asp:RequiredFieldValidator>
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description" SortExpression="Description">
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" MaxLength="250" TextMode="MultiLine" Text='<%# Bind("Description") %>'></asp:TextBox>
                <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox2" ErrorMessage="Description is required."
                                            Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert">
                </asp:RequiredFieldValidator>
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Unit Buying Price" SortExpression="UnitBuyingPrice">
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" TextMode="Number" min="1" Width="75" Text='<%# Bind("UnitBuyingPrice") %>'></asp:TextBox>
                <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox3" ErrorMessage="Unit Buying Price is required."
                                            Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert">
                </asp:RequiredFieldValidator>
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Reorder Level" SortExpression="ReorderLevel">
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server" TextMode="Number" min="1" Width="75" Text='<%# Bind("ReorderLevel") %>'></asp:TextBox>
                <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox4" ErrorMessage="Reorder Level is required."
                                            Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert">
                </asp:RequiredFieldValidator>
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowInsertButton="True" ValidationGroup="Insert" InsertText="Register"
                          ShowCancelButton="False"/>
        <asp:TemplateField ShowHeader="False">
            <ItemStyle BackColor="#E2DED6"/>
            <InsertItemTemplate>
                <asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" HeaderText="<b>Please fix following error(s):</b>"
                                       ValidationGroup="Insert" ForeColor="Red" runat="server"/>
            </InsertItemTemplate>
        </asp:TemplateField>
    </Fields>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"/>
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333"/>
</asp:DetailsView>
<br/>
<fieldset style="padding: 0.5em; width: 35em;">
    <legend>Import data from CSV</legend>
    <asp:FileUpload ID="FileUpload1" accept=".csv" BackColor="#5D7B9D" ForeColor="white"
                    Style="-ms-border-radius: 0.5em; border-radius: 0.5em; outline: none; padding: 0.5em;"
                    runat="server"/>
    <asp:Button ID="Button2" OnClick="Button2_OnClick_" Style="-ms-border-radius: 0.5em; border-radius: 0.5em; outline: none; padding: 0.5em;" runat="server" Text="Import"/>
    <asp:Label ID="lblImport" runat="server"></asp:Label>
</fieldset>
<asp:EntityDataSource ID="RawMaterialTypeSource" runat="server" ConnectionString="name=XYZDBContext"
                      DefaultContainerName="XYZDBContext" EnableDelete="True" EnableFlattening="False"
                      EnableInsert="True" EnableUpdate="True" EntitySetName="RawMaterialTypes" EntityTypeFilter="RawMaterialTypes">
</asp:EntityDataSource>
</div>
</asp:Content>